package com.zw.yzk.kabu.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.CosplayListAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.internal.component.DaggerTopicListComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.TopicListModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.CosplayModel;
import com.zw.yzk.kabu.presenter.TopicListPresenter;
import com.zw.yzk.kabu.view.view.TopicListView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class TopicListActivity extends BaseActivity implements TopicListView, CosplayListAdapter.OnItemClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.cos_list)
    RecyclerView cosList;
    @Bind(R.id.empty)
    View empty;

    private String title;

    @Inject
    TopicListPresenter presenter;
    private CosplayListAdapter cosAdapter;

    public static void startTopicActivity(Context context, String topic) {
        Intent intent = new Intent(context, TopicListActivity.class);
        intent.putExtra(ActivityConstant.TOPIC, topic);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_topic_list;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        title = getIntent().getStringExtra(ActivityConstant.TOPIC);
        String showTitle = title;
        if(!title.startsWith("@") || !title.endsWith(" ")) {
            showTitle = "#" + showTitle + "#";
        }
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(showTitle)
                .build();
    }

    @Override
    public void initComponent() {
        DaggerTopicListComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .topicListModule(new TopicListModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
        getTopicList(0, 0);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getTopicListSuccess(List<CosplayModel> modelList) {
        if (refreshLayout.isLoading()) {
            cosAdapter.add(modelList);
            refreshLayout.loadMoreFinish(modelList.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            cosAdapter.refresh(modelList);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
        //是否显示空提示
        refreshEmptyStatus(cosAdapter.getItemCount() == 0);
    }

    @Override
    public void getTopicListFail() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    @Override
    public void onPraiseClicked(CosplayModel model) {
        praise(model.getId(), model.isPraise());
    }

    @Override
    public void onCollectClicked(CosplayModel model) {
        collect(model.getId(), model.isCollected());
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        cosList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        cosAdapter = new CosplayListAdapter(null);
        cosAdapter.setOnItemClickedListener(this);
        cosList.setAdapter(cosAdapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getTopicList(0, 0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getTopicList(cosAdapter.getItemCount(), cosAdapter.getTotalList().get(cosAdapter.getItemCount() - 1).getId());
                    }
                }).build();
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取cosplay列表
     */
    private void getTopicList(int start, int lastId) {
        presenter.getTopicList(start, lastId, title);
    }

    /**
     * 是否点赞
     *
     * @param cosplayId cosid
     * @param praise    true：点赞，false：取消
     */
    public void praise(int cosplayId, boolean praise) {
        presenter.praise(cosplayId, praise);
    }

    /**
     * 是否收藏
     *
     * @param cosplayId cosId
     * @param collect   true：收藏，false：取消
     */
    public void collect(int cosplayId, boolean collect) {
        presenter.collect(cosplayId, collect);
    }
}
